package com.yufuchang;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * Created by yufuchang on 2020/9/8.
 */
@RestController
public class ErrorController {

    @Autowired
    private RestTemplate restTemplate;


    @GetMapping("/get_error")
    public String error(){
        String url = "http://localhost:8088/abc";
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        // 判断请求是否发生异常
        if(!responseEntity.getStatusCode().is2xxSuccessful()){
            // 返回异常信息
            return "请求失败，异常信息：" + responseEntity.getBody();
        }
        // 没有异常的话则返回正常的响应结果
        return responseEntity.getBody();
    }
}
